package cn.mumzone.pos.ui.adapter;

import android.content.Context;
import android.graphics.Color;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RadioButton;

import cn.mumzone.basic.adapter.rv.CommonAdapter;
import cn.mumzone.basic.adapter.rv.ViewHolder;
import cn.mumzone.basic.util.AppDateUtil;
import cn.mumzone.basic.util.StringUtils;
import cn.mumzone.pos.R;
import cn.mumzone.pos.model.ReserveTimeEntity;

/**
 * Created by qiuzhenhuan on 2019/7/6.
 */


public class ADA_ReserveTime extends CommonAdapter<ReserveTimeEntity> {


    private ViewHolder mPreHolder;

    private String mDefaultSelectTime;

    private String mPreSelectedDay;

    private String mSelectedTime;


    public ADA_ReserveTime(Context context) {
        super(context);
    }


    @Override
    protected void convert(final ViewHolder holder, final ReserveTimeEntity item, int position) {

        final RadioButton mCurrentView = (RadioButton) holder.getConvertView();


        mCurrentView.setText(item.getMinuteName());

        //默认选中
        if (mPreHolder == null && !StringUtils.isEmpty(mSelectedTime) && mSelectedTime.equals(item.getMinuteName())
                && !StringUtils.isEmpty(mPreSelectedDay)
                && AppDateUtil.isSameDay(AppDateUtil.timeStamp2Date(mDefaultSelectTime), AppDateUtil.timeStamp2Date(mPreSelectedDay))) {

            mCurrentView.setEnabled(true);
            mPreHolder = holder;
        } else {
            // 根据是否可用更新样式
            mCurrentView.setEnabled(item.isEnable());
        }

        // 点击事件
        mCurrentView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 点击同一条目不做响应
                if (mPreHolder == holder) return;

                // 首次选择
                if (mPreHolder == null) {
                    RadioButton radioButton = (RadioButton) holder.getConvertView();
                    radioButton.setChecked(true);
                    mPreHolder = holder;
                    // 点击事件
                    if (mListener != null) {
                        mListener.onItemSelected(mCurrentView);
                    }
                    return;
                }

                // 当前点击条目
                RadioButton radioButton = (RadioButton) holder.getConvertView();
                radioButton.setChecked(true);

                // 重置之前条目
                RadioButton preButton = (RadioButton) mPreHolder.getConvertView();
                preButton.setChecked(false);

                mPreHolder = holder;

                // 点击事件
                if (mListener != null) {
                    mListener.onItemSelected(mCurrentView);
                }
            }
        });
    }

    public void setDefaultSelectTime(String defaultSelectTime) {
        this.mDefaultSelectTime = defaultSelectTime;
        mSelectedTime = AppDateUtil.getTimeStamp(Long.parseLong(defaultSelectTime), AppDateUtil.HH_MM);
        mPreHolder = null;
        notifyDataSetChanged();
    }

    public void setPreSelectedDay(String preSelectedDay) {
        this.mPreSelectedDay = preSelectedDay;
    }


    public void clearCheckStatus() {

        if (mPreHolder == null) return;

        // 重置之前条目
        RadioButton preButton = (RadioButton) mPreHolder.getConvertView();
        preButton.setChecked(false);
        mDefaultSelectTime = null;
        mSelectedTime = null;
        mPreHolder = null;

    }


    public interface OnItemSelectedListener {
        void onItemSelected(View view);
    }

    private OnItemSelectedListener mListener;

    public void setOnItemSelectedListener(OnItemSelectedListener mListener) {
        this.mListener = mListener;
    }

    /**
     * 获取当前选中的条目
     *
     * @return
     */
    public ReserveTimeEntity getSelectedTime() {
        if (mPreHolder == null) return null;
        int itemPosition = mPreHolder.getAdapterPosition();
        return mDatas.get(itemPosition);
    }


    @Override
    protected int itemLayoutId() {
        return R.layout.item_update_reserve_time;
    }


}
